<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片主体区 -->
    <el-card class="box-card">
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="querryInfo.query" @clear="getUserList" clearable>
            <el-button slot="append" icon="el-icon-search" @click="getUserList"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible = true">添加用户</el-button>
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="userList" stripe>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="账号" prop="account"></el-table-column>
        <el-table-column label="用户名" prop="username"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="修改用户信息" placement="top" :enterable="false">
              <!-- 修改按钮 -->
              <el-button type="primary" icon="el-icon-edit" @click="showDialog(scope.row.id)"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="删除用户" placement="top" :enterable="false">
              <!-- 删除按钮 -->
              <el-button type="danger" icon="el-icon-delete" @click="deleteUser(scope.row.id)"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="querryInfo.current" :page-sizes="[1, 2, 5, 10]" :page-size="querryInfo.size" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-card>

    <!-- 添加用户的对话框 -->
    <el-dialog title="添加用户" :visible.sync="addDialogVisible" width="50%" @close="addDialogClosed">
      <!-- 内容主题区域 -->
      <div>
        <el-form ref="addUserformRef" :model="addUserForm" label-width="70px" :rules="addUserFormRules">
          <el-form-item label="账号" prop="account">
            <el-input v-model="addUserForm.account"></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addUserForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addUserForm.password"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改用户的对话框 -->
    <el-dialog title="修改用户" :visible.sync="editDialogVisible" width="50%" @close="editDialogClosed">
      <!-- 内容主题区域 -->
      <div>
        <el-form ref="editUserformRef" :model="editUserForm" label-width="70px" :rules="editUserFormRules">
          <el-form-item label="账号" prop="account">
            <el-input v-model="editUserForm.account" disabled></el-input>
          </el-form-item>
          <el-form-item label="用户名" prop="username">
            <el-input v-model="editUserForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="editUserForm.password"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateUser">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 获取用户列表的参数对象
      querryInfo: {
        query: '',
        account: 3,
        // 当前页码
        current: 1,
        // 每页显示记录数
        size: 2
      },
      userList: [],
      // 记录条数
      total: 0,
      // 控制添加用户对话框的显示与隐藏
      addDialogVisible: false,
      // 控制修改用户对话框的显示与隐藏
      editDialogVisible: false,
      // 添加用户的表单数据
      addUserForm: {
        account: '',
        username: '',
        password: ''
      },
      // 修改用户的表单数据
      editUserForm: {
        id: '',
        account: '',
        username: '',
        password: ''
      },
      // 添加表单的验证规则对象
      addUserFormRules: {
        account: [
          {
            required: true,
            message: '请输入账号',
            trigger: 'blur'
          },
          {
            min: 3, max: 11, message: '账号不能超过11位', trigger: 'blur'
          }
        ],
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      },
      // 修改用户表单的校验规则
      editUserFormRules: {
        account: [
          {
            required: true,
            message: '请输入账号',
            trigger: 'blur'
          },
          {
            min: 3, max: 11, message: '账号不能超过11位', trigger: 'blur'
          }
        ],
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur'
          },
          {
            min: 3,
            max: 5,
            message: '长度在 3 到 5 个字符',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            min: 6,
            max: 15,
            message: '长度在 6 到 15 个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created () {
    this.getUserList()
  },
  methods: {
    async getUserList () {
      const { data: res } = await this.$http.get('/user-do/list', {
        params: this.querryInfo
      })
      // console.log(res)
      if (!res.success) return this.$message.error('获取用户列表失败')
      this.userList = res.data
      this.total = res.total
    },
    // 监听 size 改变的事件
    handleSizeChange (newSize) {
      // 改变后重新赋值，然后重新发起请求
      this.querryInfo.size = newSize
      this.getUserList()
    },
    // 监听current 改变的事件
    handleCurrentChange (newCurrent) {
      this.querryInfo.current = newCurrent
      this.getUserList()
    },
    // 监听用户对话框的关闭事件
    addDialogClosed () {
      this.$refs.addUserformRef.resetFields()
    },
    // 监听用户对话框的确定按钮,添加新用户
    addUser () {
      this.$refs.addUserformRef.validate(async valid => {
        if (!valid) return
        // 用户表单预校验通过，可以发起网络请求添加用户
        const { data: res } = await this.$http.post('/user-do/add', this.addUserForm)
        console.log(res)
        if (!res.success) this.$message.error('添加用户失败')
        this.$message.success('添加用户成功')
        // 隐藏添加用户对话框
        this.addDialogVisible = false
        // 重新获取用户列表
        this.getUserList()
      })
    },
    // 监听删除按钮点击事件
    async deleteUser (id) {
      console.log(id)
      const confirmResult = await this.$confirm('此操作将永久删除该用户, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        center: true
      }).catch(err => err)
      // 用户点击取消
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      // 点击确定
      console.log('确定')
      const { data: res } = await this.$http.delete('/user-do/delete/' + id)
      if (!res.success) this.$message.error('删除失败')
      this.$message.success('删除成功')
      this.getUserList()
    },
    // 控制修改用户对话框的数据显示
    async showDialog (id) {
      const { data: res } = await this.$http.get('/user-do/get/' + id)
      console.log(res)
      this.editUserForm = res.data
      this.editDialogVisible = true
    },
    // 监听修改用户对话框的关闭
    editDialogClosed () {
      this.$refs.editUserformRef.resetFields()
    },
    // 更新用户
    updateUser () {
      // 表单预校验
      this.$refs.editUserformRef.validate(async valid => {
        if (!valid) return
        // 发起请求
        const { data: res } = await this.$http.post('/user-do/update/' + this.editUserForm.id, this.editUserForm)
        if (!res.success) this.$message.error('修改失败')
        // 关闭对话框
        this.editDialogVisible = false
        // 提醒用户更新成功
        this.$message.success('修改成功')
        // 刷新数据
        this.getUserList()
      })
    }
  }
}
</script>

<style lang="less" scoped></style>
